package com.erp.shop.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.erp.shop.domain.bo.ErpSellerStorePriceTemplateBo;
import com.erp.shop.domain.vo.ErpSellerStorePriceTemplateVo;
import com.erp.shop.mapper.ErpSellerStorePriceTemplateMapper;
import com.erp.shop.service.IErpSellerStorePriceTemplateService;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.oldUtils.StringUtils;
import com.ruoyi.common.core.web.oldPage.TableDataInfo;
import com.ruoyi.common.security.utils.SecurityUtils;
import com.ruoyi.shop.api.domain.ErpSellerStorePriceTemplate;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 智能定价模板Service业务层处理
 *
 * @author erp
 * @date 2023-05-04
 */
@RequiredArgsConstructor
@Service
public class ErpSellerStorePriceTemplateServiceImpl implements IErpSellerStorePriceTemplateService {

    private final ErpSellerStorePriceTemplateMapper baseMapper;

    /**
     * 查询智能定价模板
     */
    @Override
    public ErpSellerStorePriceTemplateVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询智能定价模板列表
     */
    @Override
    public TableDataInfo<ErpSellerStorePriceTemplateVo> queryPageList(ErpSellerStorePriceTemplateBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<ErpSellerStorePriceTemplate> lqw = buildQueryWrapper(bo);
        Page<ErpSellerStorePriceTemplateVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询智能定价模板列表
     */
    @Override
    public List<ErpSellerStorePriceTemplateVo> queryList(ErpSellerStorePriceTemplateBo bo) {
        LambdaQueryWrapper<ErpSellerStorePriceTemplate> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<ErpSellerStorePriceTemplate> buildQueryWrapper(ErpSellerStorePriceTemplateBo bo) {
        LambdaQueryWrapper<ErpSellerStorePriceTemplate> lqw = Wrappers.lambdaQuery();
        if (bo.getSellerId() == null ){
            lqw.eq(ErpSellerStorePriceTemplate::getSellerId, SecurityUtils.getUserId());
        }else {
            lqw.eq(bo.getSellerId() != null, ErpSellerStorePriceTemplate::getSellerId, bo.getSellerId());
        }
        lqw.like(StringUtils.isNotBlank(bo.getTemplateName()), ErpSellerStorePriceTemplate::getTemplateName, bo.getTemplateName());
        lqw.eq(bo.getDiscount() != null, ErpSellerStorePriceTemplate::getDiscount, bo.getDiscount());
        lqw.eq(bo.getUnion() != null, ErpSellerStorePriceTemplate::getUnion, bo.getUnion());
        lqw.eq(bo.getTransaction() != null, ErpSellerStorePriceTemplate::getTransaction, bo.getTransaction());
        lqw.eq(bo.getProfit() != null, ErpSellerStorePriceTemplate::getProfit, bo.getProfit());
        lqw.eq(bo.getAddTime() != null, ErpSellerStorePriceTemplate::getAddTime, bo.getAddTime());
        return lqw;
    }

    /**
     * 新增智能定价模板
     */
    @Override
    public Boolean insertByBo(ErpSellerStorePriceTemplateBo bo) {
        ErpSellerStorePriceTemplate add = BeanUtil.toBean(bo, ErpSellerStorePriceTemplate.class);
        validEntityBeforeSave(add);
        add.setSellerId(SecurityUtils.getUserId());
        add.setAddTime(new Date());
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改智能定价模板
     */
    @Override
    public Boolean updateByBo(ErpSellerStorePriceTemplateBo bo) {
        ErpSellerStorePriceTemplate update = BeanUtil.toBean(bo, ErpSellerStorePriceTemplate.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(ErpSellerStorePriceTemplate entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除智能定价模板
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public List<ErpSellerStorePriceTemplateVo> selectListBySellerId(String sellerId) {
        return baseMapper.selectListBySellerId(sellerId);
    }

    @Override
    public R<Object> selectInfoById(Long id) {
        ErpSellerStorePriceTemplate priceTemplate = baseMapper.selectById(id);
        if (Objects.isNull(priceTemplate)) {
            return R.fail("定价模版为空");
        } else {
            return R.ok(priceTemplate);
        }
    }

    @Override
    public ErpSellerStorePriceTemplate selectById(Long id) {
        return baseMapper.selectById(id);
    }

    @Override
    public ErpSellerStorePriceTemplate sellerStorePriceTemplateBySellerId(Long sellerId) {
        ErpSellerStorePriceTemplate erpSellerStorePriceTemplate = baseMapper.selectOne(
                new LambdaQueryWrapper<ErpSellerStorePriceTemplate>().eq(ErpSellerStorePriceTemplate::getSellerId, sellerId).last("limit 1")
        );
        return erpSellerStorePriceTemplate;
    }
}
